package biznussoft.metercor.fragments;

import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import biznussoft.metercor.MainActivity;
import biznussoft.metercor.R;
import biznussoft.metercor.listener.Globallistener;
import biznussoft.metercor.lookup.SiteLookUp;
import biznussoft.metercor.utils.Session;

import com.salesforce.androidsdk.rest.RestClient;
import com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback;
import com.salesforce.androidsdk.rest.RestRequest;
import com.salesforce.androidsdk.rest.RestResponse;
import com.salesforce.androidsdk.util.EventsObservable;
import com.salesforce.androidsdk.util.EventsObservable.EventType;

public class NewStockTransferFragment extends Fragment implements OnTouchListener, OnClickListener{
	public static  EditText source,destination,additionalInformation;
	public ImageView save;
	private Globallistener listener;
	private RestClient client;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.new_stocktransfer, null);
	}
	
	@Override
	public void onAttach(Activity activity) {
		listener = (Globallistener) getActivity();
		super.onAttach(activity);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		client = MainActivity.mActivity.client;
		source = (EditText) getView().findViewById(R.id.etSource);
		destination = (EditText) getView().findViewById(R.id.etDestination);
		additionalInformation = (EditText) getView().findViewById(R.id.etAdditional);
		source.setOnTouchListener(this);
		save = (ImageView) getView().findViewById(R.id.btsave);
		save.setOnClickListener(this);
		destination.setOnTouchListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btsave:
			newStockTransfer();
			break;

		default:
			break;
		}

	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {

		case R.id.etSource:
			if(event.getAction() == MotionEvent.ACTION_UP){
				SiteLookUp lookUp = new SiteLookUp();
				lookUp.accountLookupDetails(getActivity(), "source");
			}
			break;
		case R.id.etDestination:
			if(event.getAction() == MotionEvent.ACTION_UP){
				SiteLookUp lookUp = new SiteLookUp();
				lookUp.accountLookupDetails(getActivity(), "destination");
			}
			break;
		}
		return false;
	}

	public void newStockTransfer(){
		String objectType = "FConnect__Stock_Transfer__c";                                 
		HashMap<String, Object> fields = new HashMap<String, Object>();
		fields.put("FConnect__Source_Location__c",Session.sSiteSourceId);                  
		fields.put("FConnect__Destination_Location__c",Session.sSiteDestinaltionId);
		fields.put("FConnect__Additional_Information__c",additionalInformation.getText().toString());

		try {                                    
			RestRequest restRequest = RestRequest.getRequestForCreate(getString(R.string.api_version),objectType,fields);
			client.sendAsync(restRequest, new AsyncRequestCallback() {
				@Override
				public void onSuccess(RestRequest request, RestResponse result) {
					try {   

						if(result.toString().contains("success")){
							Toast.makeText(getActivity(), "Stock Transferred Saved Successfully." , 1000).show();
							listener.getSignature("Inserted");
							source.setText("");
							destination.setText("");
							additionalInformation.setText("");
							
						}

					}                                
					catch (Exception e) {
						Log.i("TPT","Exception is   :"+e.getMessage());                      
					}
				}

				//@Override
				public void onError(Exception exception) {
					EventsObservable.get().notifyEvent(EventType.RenditionComplete);
				}
			});
		} 
		catch (Exception e) {
			Log.i("NCS","Exception is   :"+e.getMessage());

		}



	}

}
